﻿Imports System.Data.SqlClient
Public Class FrmEmpleados
    Dim controller As New Class_RC_Controller.rc_Empleados

    Private _codemp As Integer = 0

    Public Property codemp() As Integer
        Get
            Return _codemp
        End Get
        Set(ByVal value As Integer)
            _codemp = value
            txtcodigo.Text = value.ToString().PadLeft(4, "0")
        End Set
    End Property
    Private m_frmEmpleados As FrmEmpleados
    Public Property frmEmpleados() As FrmEmpleados
        Get
            Return m_frmEmpleados
        End Get
        Set(ByVal value As FrmEmpleados)
            m_frmEmpleados = value
        End Set
    End Property

    Public Sub limpiar()
        Me.txtnumero.Text = ""
        Me.txtnombre.Text = ""
        Me.txtapellido.Text = ""
        Me.txtdireccion.Text = ""
        Me.txttelefono.Text = ""
        Me.txtcelular.Text = ""
        Me.txtcedula.Text = ""
        Me.txtnrolegajo.Text = ""
        Me.cmbciudad.Text = ""
        Me.cmbcategoria.Text = ""
        Me.cmbtipoemp.Text = ""
        Me.dtpfechaingreso.Text = ""
        Me.dtpfechanac.Text = ""

    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs)


    End Sub

    

    
    Private Shadows Sub KeyUpmask(ByVal MaskedTextboxs As System.Windows.Forms.MaskedTextBox, ByVal e As System.Windows.Forms.KeyEventArgs)

        'Teclas que Utilizaremos El cual solo son las numericas
        If e.KeyData = Keys.Space Or e.KeyData = Keys.Decimal Or _
         e.KeyData = Keys.NumPad0 Or e.KeyData = Keys.NumPad1 Or e.KeyData = Keys.NumPad2 Or e.KeyData = Keys.NumPad3 _
           Or e.KeyData = Keys.NumPad4 Or e.KeyData = Keys.NumPad5 Or e.KeyData = Keys.NumPad6 Or _
           e.KeyData = Keys.NumPad7 Or e.KeyData = Keys.NumPad8 Or e.KeyData = Keys.NumPad9 _
           Or e.KeyData = Keys.D0 Or e.KeyData = Keys.D1 Or e.KeyData = Keys.D2 Or e.KeyData = Keys.D3 Or _
           e.KeyData = Keys.D4 Or e.KeyData = Keys.D5 Or e.KeyData = Keys.D6 _
           Or e.KeyData = Keys.D7 Or e.KeyData = Keys.D8 Or e.KeyData = Keys.D9 Or e.KeyData = Keys.Tab Or e.KeyData = Keys.Enter _
            Then

            'Programamos la tecla de Retroceso en el textbox
        ElseIf e.KeyData = Keys.Back And MaskedTextboxs.Text.Length > 0 Then
            On Error Resume Next
            'Removemos un caracter mediante se presione la tecla Back
            MaskedTextboxs.Text.Remove(MaskedTextboxs.SelectionStart - 1)

            'si el textbox es menor que un caracter
        ElseIf MaskedTextboxs.Text.Length < 1 Then
            'ponemos el textbox En Blanco
            MaskedTextboxs.Text = String.Empty
            'ponemos la variable    NumerosYaescritos en Blanco

        Else
            'ponemos el textbox enblanco
            MaskedTextboxs.Text = String.Empty
            'Hacemos Sonar un Beep para indicarle al usuario que ha presionado un caracter y no un Numero
            Beep()
            MsgBox("Introducir solo valores numericos")
        End If

    End Sub


    Private Sub txttelefono_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        'Validar que solo se ingresen valores numericos
        Me.KeyUpmask(Me.txttelefono, e)

    End Sub


    Private Sub txtcelular_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        'Validar que solo se ingresen valores numericos
        Me.KeyUpmask(Me.txtcelular, e)
    End Sub
    Private Shadows Sub KeyUp(ByVal TextBoxs As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyEventArgs)

        'Teclas que Utilizaremos El cual solo son las numericas
        If e.KeyData = Keys.Space Or e.KeyData = Keys.Decimal Or _
         e.KeyData = Keys.NumPad0 Or e.KeyData = Keys.NumPad1 Or e.KeyData = Keys.NumPad2 Or e.KeyData = Keys.NumPad3 _
           Or e.KeyData = Keys.NumPad4 Or e.KeyData = Keys.NumPad5 Or e.KeyData = Keys.NumPad6 Or _
           e.KeyData = Keys.NumPad7 Or e.KeyData = Keys.NumPad8 Or e.KeyData = Keys.NumPad9 _
           Or e.KeyData = Keys.D0 Or e.KeyData = Keys.D1 Or e.KeyData = Keys.D2 Or e.KeyData = Keys.D3 Or _
           e.KeyData = Keys.D4 Or e.KeyData = Keys.D5 Or e.KeyData = Keys.D6 _
           Or e.KeyData = Keys.D7 Or e.KeyData = Keys.D8 Or e.KeyData = Keys.D9 Or e.KeyData = Keys.Tab Or e.KeyData = Keys.Enter _
           Or e.KeyData = Keys.OemMinus Then

            'Programamos la tecla de Retroceso en el textbox
        ElseIf e.KeyData = Keys.Back And TextBoxs.Text.Length > 0 Then
            On Error Resume Next
            'Removemos un caracter mediante se presione la tecla Back
            TextBoxs.Text.Remove(TextBoxs.SelectionStart - 1)

            'si el textbox es menor que un caracter
        ElseIf TextBoxs.Text.Length < 1 Then
            'ponemos el textbox En Blanco
            TextBoxs.Text = String.Empty
            'ponemos la variable    NumerosYaescritos en Blanco

        Else
            'ponemos el textbox enblanco
            TextBoxs.Text = String.Empty
            'Hacemos Sonar un Beep para indicarle al usuario que ha presionado un caracter y no un Numero
            Beep()
            MsgBox("Introducir solo valores numericos")
        End If

    End Sub

    Private Sub txtcedula_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtcedula.KeyUp
        'Validar que solo se ingresen valores numericos
        Me.KeyUp(Me.txtcedula, e)
    End Sub

    Private Sub FrmEMPLEADOS_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.llenarCombos()
        Me.CargarEmpleado()

    End Sub

    'Procedimientos para pasar al siguiente textbox presionando ENTER

    Private Sub txtnombre_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtnombre.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtapellido_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtapellido.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtdireccion_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtdireccion.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub cmbciudad_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbciudad.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txttelefono_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txttelefono.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtcelular_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtcelular.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
    Private Sub txtcedula_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtcedula.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub dtpfechanac_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles dtpfechanac.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtnrolegajo_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtnrolegajo.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub cmbtipoemp_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbtipoemp.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub cmbcategoria_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbcategoria.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub dtpfechaingreso_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles dtpfechaingreso.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
    Private Sub llenarCombos()

        Dim dstipoemp As DataTable
        dstipoemp = controller.get_tipo_Empleado(oiml.dbConn)
        With Me.cmbtipoemp
            .DataSource = dstipoemp
            .DisplayMember = "DescTipoEmp"
            .ValueMember = "codTipoEmp"
        End With
        Me.llenarcmbcategoria()

        Dim dsCiudad As DataTable
        dsCiudad = controller.get_Ciudades(oiml.dbConn)
        With Me.cmbciudad
            .DataSource = dsCiudad
            .DisplayMember = "descripcion"
            .ValueMember = "codCiudad"
        End With

        
    End Sub
    Private Sub llenarcmbcategoria()
        If (IsNumeric(cmbtipoemp.SelectedValue)) Then
            Dim dscategoria As DataTable
            dscategoria = controller.get_Categoria(oiml.dbConn, cmbtipoemp.SelectedValue)
            With Me.cmbcategoria
                .DataSource = dscategoria
                .DisplayMember = "DescCategoria"
                .ValueMember = "codCategoria"
            End With
        End If
       
    End Sub
    Private Sub CargarEmpleado()
        Try
            If codemp > 0 Then
                Dim ds As New DataTable
                ds = controller.get_Empleados_All(oiml.dbConn, txtcodigo.Text, "", "", 0, 0, 0)

                If Not ds Is Nothing Then
                    ' cargar los controles del formulario con los valores de los campos del registro
                    'Me.txtcodcliente.Text = ds.Rows(0)("codCliente").ToString
                    codemp = ds.Rows(0)("codEmp").ToString
                    Me.txtnumero.Text = ds.Rows(0)("NroEmp")
                    Me.txtnombre.Text = ds.Rows(0)("Nombre").ToString
                    Me.txtapellido.Text = ds.Rows(0)("Apellido").ToString
                    Me.txtdireccion.Text = ds.Rows(0)("Direccion").ToString
                    Me.txttelefono.Text = ds.Rows(0)("telefono").ToString
                    Me.txtcelular.Text = ds.Rows(0)("celular").ToString
                    Me.txtcedula.Text = ds.Rows(0)("NroCi")
                    Me.dtpfechanac.Text = ds.Rows(0)("FechaNac").ToString
                    Me.cmbciudad.SelectedValue = ds.Rows(0)("codCiudad").ToString
                    Me.txtnrolegajo.Text = ds.Rows(0)("NroLegajo")
                    Me.cmbtipoemp.SelectedValue = ds.Rows(0)("CodTipoEmp").ToString
                    Me.cmbcategoria.SelectedValue = ds.Rows(0)("CodCategoria").ToString
                    Me.dtpfechaingreso.Text = ds.Rows(0)("FechaIngreso").ToString
                    'lblUsuario.Text = ds.Rows(0)("usuario").ToString
                    'lblfechaActualizacion.Text = ds.Rows(0)("fechaActualizacion_").ToString
                End If

            Else

                Me.txtcodigo .Text = ""
                Me.txtnombre.Text = ""
                Me.txtapellido.Text = ""
                Me.txtdireccion.Text = ""
                Me.txttelefono.Text = ""
                Me.txtcelular.Text = ""
                txtnombre.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False
        If txtnombre.Text = "" Then
            'ErrorProvider(txtnombre, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtnombre, "")
        End If

        
        Return controlCampos

    End Function

    'Private Sub ErrorProvier(ByVal textBox As TextBox, ByVal p2 As String)
    '    Throw New NotImplementedException
    'End Sub

    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub


    Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If ValidarCampos() Then
            txtnombre.Focus()
            Exit Sub
        End If

        If codemp = 0 Then

            codemp = controller.Insert_Empleado(oiml.dbConn, 0, txtnumero.Text, txtnombre.Text, txtapellido.Text, txtdireccion.Text, cmbciudad.SelectedValue, txttelefono.Text, txtcelular.Text, txtcedula.Text,
                                                dtpfechaingreso.Value, cmbtipoemp.SelectedValue, cmbcategoria.SelectedValue, txtnrolegajo.Text, dtpfechanac.Value)

            If codemp > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Empleados", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Empleados", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If controller.Update_Empleado(oiml.dbConn, txtcodigo.Text, txtnumero.Text, txtnombre.Text, txtapellido.Text, txtdireccion.Text, cmbciudad.SelectedValue,
                                            txttelefono.Text, txtcelular.Text, txtcedula.Text, dtpfechaingreso.Value, cmbtipoemp.SelectedValue, cmbcategoria.SelectedValue,
                                           txtnrolegajo.Text, dtpfechanac.Value) Then

                MessageBox.Show("Registro grabado con exito!", "Empleados", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Empleados", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

        limpiar()

    End Sub


    Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Try
            If codemp > 0 Then
                If MessageBox.Show("Desea eliminar el registro seleccionado?", "Empleados", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                    If controller.Delete_Empleado(oiml.dbConn, codemp) Then
                        Me.Close()

                    Else
                        MessageBox.Show("Error: " & controller.MessageError.Message, "Empleado", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    End If

                End If
            End If


        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub
    Public Sub NuevoEmpleado()
        txtnumero.Focus()
    End Sub

    Private Sub cmbtipoemp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbtipoemp.SelectedIndexChanged
        llenarcmbcategoria()
    End Sub
End Class